<?php
require("../inc/constants.inc.php");
// define variables and set to empty values
$nameErr = $emailErr = $rollNumberErr = $confirmPasswordErr = $passwordErr = $genderErr = $websiteErr = $passErr = $RegistrationErr = $collegeErr = "";
$name = $email = $rollNumber = $password = $confirmPassword = $gender = $pass = $college = "";

if ($_SERVER["REQUEST_METHOD"] == "POST")
{
	$con=mysqli_connect(DB_HOST,DB_USERNAME,"",DB_NAME);
    // Check connection
	if (mysqli_connect_errno())
	{
		echo "Failed to connect to MySQL: " . mysqli_connect_error();
	}
		
	$name = fiter_input($_POST["name"]);
	$email = fiter_input($_POST["email"]);
	$experience = fiter_input($_POST["experience"]);
	$domain = fiter_input($_POST["domain"]);
	$company = fiter_input($_POST["company"]);
	$mobileNo = fiter_input($_POST["mobileNo"]);
	$referrer = fiter_input($_POST["referrer"]);
	$location = fiter_input($_POST["location"]);
	$employeeId = fiter_input($_POST["employeeId"]);	
	if($employeeId=="" || empty($employeeId))
	{
		$employeeId = 0;
	}	
	$employeeName = fiter_input($_POST["employeeName"]);
	if($employeeName=="" || empty($employeeName))
	{
		$employeeName = none;
	}
	
	//$rollNumber = fiter_input($_POST["rollNumber"]);
	//$college = fiter_input($_POST["college"]);
	$password = md5(mysql_real_escape_string($_POST["password"]));
	$confirmPassword = md5(mysql_real_escape_string($_POST["confirmPassword"]));
	$gender = fiter_input($_POST["gender"]);
	$userrole = 2;	 // for student
	
	// Validation goes here
	/*
	if($name!= "")
	{
		RegistrationErr = 1; 
	}
	*/
	
	// Check for duplicate email
	
	
	// Insert into database
	//if($name!= "" && $email != "" && $rollNumber != "" && $gender != "" && $password != "" && $college != "" && $RegistrationErr == ""){
     if($name!= "" && $email != "" && $experience != "" && $gender != "" && $password != "" && $referrer != "" && $location != "" && $mobileNo != "" && $company != "" && $domain != "" && $RegistrationErr == ""){
		//$sql="INSERT INTO login_details (UserName,UserEmail,UserCollege,UserRollNo,UserGender,UserPassword,UserRole)  VALUES ('$name','$email','$college','$rollNumber','$gender','$password','$userrole')";
		$sql="INSERT INTO login_details (UserName,UserEmail,experience,company,referrer,location,employeeId,employeeName,mobileNo,domain,UserGender,UserPassword,UserRole,submitted)  VALUES ('$name','$email','$experience','$company','$referrer','$location','$employeeId','$employeeName','$mobileNo','$domain','$gender','$password','$userrole','no')";
		if (!mysqli_query($con,$sql))
		{
			die('Error: ' . mysqli_error($con));
		}
		else
		{
			header("Location: ../login.php?success=1");
			exit(1);
		}

	 }else
	 {
		header("Location: ../index.php?error=1");
	 }
	 
}else
	 {
		header("Location: ../index.php?invalidAccess=1");
}
 
 
function fiter_input($data)
{
     $data = trim(mysql_real_escape_string($data));
     $data = htmlspecialchars($data);
     return $data;
}
?>